/**
 * 
 */
package pl.net.beercrm.model;

import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;

/**
 * @author Rafi
 * 
 */
@Entity(name = "PRODUCT")
public class Product {

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;

	@Column(name = "GUID", nullable = false, unique = true, length = 36)
	private String productId;

	@Column(name = "MANUFACTURER", nullable = false, length = 30)
	private String manufacturer;

	@Column(name = "NAME", nullable = false, length = 30)
	private String name;

	@Column(name = "TYPE", nullable = false, length = 30)
	private String type;

	@Column(name = "CAPACITY", nullable = false)
	private int capacity;

	@Column(name = "BUNDLE", nullable = false, length = 30)
	private String bundle;

	@Column(name = "VOLTAGE", nullable = false)
	private double voltage;

	@Column(name = "DESCRIPTION", nullable = true, length = 255)
	private String description;

	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "PRODUCT_INGREDIENT", joinColumns = { @JoinColumn(name = "PRODUCTID", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "INGREDIENTID", nullable = false, updatable = false) })
	private Set<Ingredient> ingredients = new HashSet<Ingredient>(0);

	@Column(name = "PRICE", nullable = false, precision = 6, scale = 2)
	private BigDecimal price;

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "product")
	private Set<Purchase> purchases = new HashSet<Purchase>(0);

	public String getManufacturer() {
		return manufacturer;
	}

	public void setManufacturer(String manufacturer) {
		this.manufacturer = manufacturer;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public BigDecimal getPrice() {
		return price;
	}

	public void setPrice(BigDecimal price) {
		this.price = price;
	}

	public Set<Purchase> getPurchases() {
		return purchases;
	}

	public void setPurchases(Set<Purchase> purchases) {
		this.purchases = purchases;
	}

	public Set<Ingredient> getIngredients() {
		return ingredients;
	}

	public void setIngredients(Set<Ingredient> ingredients) {
		this.ingredients = ingredients;
	}

	public Long getId() {
		return id;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public int getCapacity() {
		return capacity;
	}

	public void setCapacity(int capacity) {
		this.capacity = capacity;
	}

	public String getBundle() {
		return bundle;
	}

	public void setBundle(String bundle) {
		this.bundle = bundle;
	}

	public double getVoltage() {
		return voltage;
	}

	public void setVoltage(double voltage) {
		this.voltage = voltage;
	}

	public String getProductId() {
		return productId;
	}

	public void setProductId(String productId) {
		this.productId = productId;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((bundle == null) ? 0 : bundle.hashCode());
		result = prime * result + capacity;
		result = prime * result
				+ ((description == null) ? 0 : description.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((price == null) ? 0 : price.hashCode());
		result = prime * result
				+ ((productId == null) ? 0 : productId.hashCode());
		result = prime * result + ((type == null) ? 0 : type.hashCode());
		result = prime * result
				+ ((manufacturer == null) ? 0 : manufacturer.hashCode());
		long temp;
		temp = Double.doubleToLongBits(voltage);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Product other = (Product) obj;
		if (bundle == null) {
			if (other.bundle != null)
				return false;
		} else if (!bundle.equals(other.bundle))
			return false;
		if (capacity != other.capacity)
			return false;
		if (description == null) {
			if (other.description != null)
				return false;
		} else if (!description.equals(other.description))
			return false;
		if (manufacturer == null) {
			if (other.manufacturer != null)
				return false;
		} else if (!manufacturer.equals(other.manufacturer))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (price == null) {
			if (other.price != null)
				return false;
		} else if (!price.equals(other.price))
			return false;
		if (productId == null) {
			if (other.productId != null)
				return false;
		} else if (!productId.equals(other.productId))
			return false;
		if (type == null) {
			if (other.type != null)
				return false;
		} else if (!type.equals(other.type))
			return false;
		if (Double.doubleToLongBits(voltage) != Double
				.doubleToLongBits(other.voltage))
			return false;
		return true;
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("Product [productId=").append(productId)
				.append(", manufacturer=").append(manufacturer)
				.append(", name=").append(name).append(", type=").append(type)
				.append(", capacity=").append(capacity).append(", bundle=")
				.append(bundle).append(", voltage=").append(voltage)
				.append(", description=").append(description)
				.append(", price=").append(price).append("]");
		return builder.toString();
	}

}
